package com.facebank.usersupport.controller;

import com.facebank.usersupport.model.Activity;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.service.IActivityService;
import com.facebank.usersupport.util.ImgSaveUtil;
import com.facebank.usersupport.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @RequestMapping("/activity/add")
    public RestModel add(Integer id) {
        return activityService.add(id);
    }

    @RequestMapping("/activity/delete")
    public RestModel delete(Integer id) {
        return activityService.delete(id);
    }

    @RequestMapping("/activity/list")
    public RestModel list(@RequestParam(required = false, defaultValue = "1") int pageNo,
                          @RequestParam(required = false, defaultValue = "6") int length,
                          String routeName, Short routeType, Short status, String starttime, String endtime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long startTime = TimeUtil.StringToLong(starttime, sdf);
        long endTime = TimeUtil.StringToLong(endtime, sdf);
        return activityService.list(pageNo, length, routeName, routeType, status, startTime, endTime);
    }

    //TODO  查看活动详情
    @RequestMapping("/activity/detail")
    public RestModel detail(Integer id) {
        return activityService.detail(id);
    }

    @RequestMapping("/activity/updata")
    public RestModel updata(Activity activity, @RequestParam(value = "dateTime", required = false) String dateTime,
                            @RequestParam(value = "file", required = false) MultipartFile file) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long startTime = TimeUtil.StringToLong(dateTime,sdf);
        activity.setDaytime(startTime);
        String path = "static/images/upload/";
        if (file != null) {
            String targetImgName = ImgSaveUtil.uploadImg(file, path);
            activity.setImg("/images/upload/" + targetImgName);
        }
        return activityService.updata(activity);
    }

    @RequestMapping("/activity/introduce")
    public RestModel introduce(Integer id) {
        return activityService.introduce(id);
    }
}
